#include "stdafx.h"
#include "SubscriptionsDlg.h"
#include "ListItemsDlg.h"
#include "LoginDlg.h"
#include "ShellNotifyMsg.h"

#define MZ_IDC_LIST			101
#define MZ_IDC_TOOLBAR		102

CSubscriptionsDlg g_SubscriptionDlg;

CSubscriptionsDlg::CSubscriptionsDlg()
{
}

CSubscriptionsDlg::~CSubscriptionsDlg()
{
}

BOOL CSubscriptionsDlg::OnInitDialog()
{
	if(!CMzWndEx::OnInitDialog()) return FALSE;

    mList.SetPos(0, 0, GetWidth(), GetHeight()-MZM_HEIGHT_TEXT_TOOLBAR);
    mList.SetID(MZ_IDC_LIST);
    mList.EnableScrollBarV(true);
    mList.EnableNotifyMessage(true);
    AddUiWin(&mList);

	mToolbar.SetPos(0, GetHeight()-MZM_HEIGHT_TEXT_TOOLBAR, GetWidth(), MZM_HEIGHT_TEXT_TOOLBAR);
    mToolbar.SetID(MZ_IDC_TOOLBAR);
    mToolbar.SetButton(0, true, true, L"Exit");
    mToolbar.SetButton(1, true, true, L"Update");
    mToolbar.SetButton(2, true, true, L"Account");
    AddUiWin(&mToolbar);

	return TRUE;
}

LRESULT CSubscriptionsDlg::MzDefWndProc(UINT message, WPARAM wParam, LPARAM lParam)
{
	if(message==MZ_WM_MOUSE_NOTIFY) {
		int nID = LOWORD(wParam);
		int nNotify = HIWORD(wParam);
		if(nID==MZ_IDC_LIST && nNotify==MZ_MN_LBUTTONDOWN) {
			int x = LOWORD(lParam);
			int y = HIWORD(lParam);
			if(!mList.IsMouseDownAtScrolling() && !mList.IsMouseMoved()) {
				int nIndex = mList.CalcIndexOfPos(x, y);
				mList.SetSelectedIndex(nIndex);
				mList.Invalidate();
				mList.Update();
			}
		}
		if(nID==MZ_IDC_LIST && nNotify==MZ_MN_MOUSEMOVE) {
			if(mList.GetSelectedIndex()!=-1) {
				mList.SetSelectedIndex(-1);
				mList.Invalidate();
				mList.Update();
			}
		}
		if(nID==MZ_IDC_LIST && nNotify==MZ_MN_LBUTTONUP) {
			if(mList.GetSelectedIndex()>=0) {
				int nSID = (int)(mList.GetItem(mList.GetSelectedIndex())->Data);
				g_ListItemsDlg.Active(MZ_ANIMTYPE_SCROLL_RIGHT_TO_LEFT_1, nSID);
			}
		}
	}

	return CMzWndEx::MzDefWndProc(message,wParam,lParam);
}

void CSubscriptionsDlg::OnMzCommand(WPARAM wParam, LPARAM lParam)
{
	UINT_PTR id = LOWORD(wParam);

	if(id==MZ_IDC_TOOLBAR) {
		switch(lParam) {
		case 0:
			if(MzGetParam(MZGP_APP_START_ANIMATION)==TRUE) {
				AnimateWindow(MZ_ANIMTYPE_ZOOM_OUT, true);
			}
			Show(false);
			PostQuitMessage(0);
			break;
		case 1:
			{
				TCHAR szUsername[100];
				TCHAR szPassword[100];
				if(		!CReaderDatabase::GetSingleton().GetValue(_T("username"), szUsername, sizeof(szUsername))
					||	!CReaderDatabase::GetSingleton().GetValue(_T("password"), szPassword, sizeof(szPassword))) {
					MzMessageBoxEx(m_hWnd, L"need username and password", L"ERROR", MZ_OK);
					break;
				}

				HoldShellBatteryLowWarningBox(m_hWnd);
				SetScreenAlwaysOn(m_hWnd);
				MzBeginWaitDlg(m_hWnd);
				UpdateDB(szUsername, szPassword);
				MzEndWaitDlg();
				SetScreenAutoOff();
				UnHoldShellBatteryLowWarningBox(m_hWnd);

				UpdateUI();

				break;
			}
			break;
		case 2:
			g_LoginDlg.Active(MZ_ANIMTYPE_SCROLL_RIGHT_TO_LEFT_1);
			break;
		}
	}
}

void CSubscriptionsDlg::Active(UINT nAnimType, bool bFlush)
{
	if(bFlush) {
		UpdateUI();
	}

    if(MzGetParam(MZGP_APP_START_ANIMATION)==TRUE) {
		AnimateWindow(nAnimType, true);
    }
	Show();
}

void CSubscriptionsDlg::UpdateUI()
{
	mList.RemoveAll();
	mList.SetSelectedIndex(-1);

	std::vector<Subscription> List;
	if(!CReaderDatabase::GetSingleton().GetList(List)) {
		MzMessageBoxEx(m_hWnd, L"ERROR GetList", L"ERROR", MZ_OK);
		return;
	}

	ListItem li;
	TCHAR szTitle[300];
	for(size_t i=0; i<List.size(); i++) {
		if(!Utf8ToTCHAR(List[i].title.c_str(), szTitle, sizeof(szTitle))) {
			_tcscpy(szTitle, _T("Utf8ToTCHAR error"));
		}
		li.Data = (void*)List[i].sid;
		li.Text = szTitle;
		mList.AddItem(li);
	}
}

void CSubscriptionsDlg::UpdateDB(LPCTSTR pUsername, LPCTSTR pPassword)
{
	if(!Service.Auth(pUsername, pPassword)) {
		MzMessageBoxEx(m_hWnd, L"ERROR Auth", L"ERROR", MZ_OK);
		return;
	}
	if(!CReaderDatabase::GetSingleton().Update(&Service)) {
		MzMessageBoxEx(m_hWnd, L"ERROR Update", L"ERROR", MZ_OK);
		return;
	}
}
